package com.taplinker.client;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
import android.util.Log;
import android.widget.Toast;

import com.taplinker.client.core.image.ImageManager;
import com.taplinker.core.ResourceManager;
import com.taplinker.core.SimpleApplication;
import com.taplinker.core.util.CrashHandler;
import com.taplinker.sdk.core.MessageManager;
import com.ti.winona.ui.WelcomeActivity;

public class App extends SimpleApplication {
	
	private static String TAG = "App";
	private static App INSTANCE;
	
	public static App getInstance() {
		return INSTANCE;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		INSTANCE = this;
		
		AppConfigFactory.getInstance().initAppConfig(getApplicationContext(),this);
		MessageManager.getInstance().initialize(this);
		ImageManager.getInstance().initialize(getApplicationContext());

		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(this);
	}
	
	public static void showToast( Object msg ) {
		Toast.makeText(getInstance(), msg + "", Toast.LENGTH_SHORT).show();
	}
	
	public static void showLog( String tag, Object msg ) {
		Log.d(TAG + "_" + tag, msg + "");
	}
	
	public void logOut() {
		ResourceManager.getInstance().releaseAll();
		showToast("连接断开, 重新登陆, 请保持网络畅通!");
		startActivity(new Intent(this, WelcomeActivity.class));
	}

	/**
	 * 后台运行
	 * 
	 * @return
	 */
	private boolean isBackgroundRunning() {
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

		if (activityManager == null) {
			return false;
		}
		// get running application processes
		List<ActivityManager.RunningAppProcessInfo> processList = activityManager
				.getRunningAppProcesses();
		for (ActivityManager.RunningAppProcessInfo process : processList) {
			if (process.pid == Process.myPid()) {
				boolean isBackground = process.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND
						&& process.importance != RunningAppProcessInfo.IMPORTANCE_VISIBLE;
				boolean isLockedState = keyguardManager
						.inKeyguardRestrictedInputMode();
				if (isBackground || isLockedState)
					return true;
				else
					return false;
			}
		}
		return false;
	}

	public void notifyMessage(String message) {
		if (isBackgroundRunning()) {
			String notifyMsg = message;
			String action = "com.taplinker.sdk.19581e27de7ced00ff1ce50b2047e7a567c76b1cbaebabe5ef03f7c3017bb5b7";

			NotificationManager notifyManager = (NotificationManager) AppConfigFactory
					.getInstance().getApplication()
					.getSystemService(Context.NOTIFICATION_SERVICE);
			Notification notice = new Notification(AppConfigFactory.getInstance().getApplication().getApplicationContext().getApplicationInfo().icon, notifyMsg, System.currentTimeMillis());
			
			notice.flags = Notification.FLAG_AUTO_CANCEL;
			
			notice.defaults = Notification.DEFAULT_SOUND;
			notice.defaults = Notification.DEFAULT_VIBRATE;
			notice.defaults = Notification.FLAG_SHOW_LIGHTS;
			
			
			Intent intent = new Intent(action);
//			intent.setClass(TaplinkerApplication.getInstance(), HomeActivity.class);
			PendingIntent pi = PendingIntent.getBroadcast(AppConfigFactory.getInstance().getApplication(), (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

			notice.contentIntent = pi;
			
			notice.setLatestEventInfo(AppConfigFactory.getInstance().getApplication(), "您有新消息", notifyMsg, pi);
			
			notifyManager.notify((int) System.currentTimeMillis(), notice);
		}
	}
}
